package ttOrder.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import ttOrder.model.vo.UserVO;
import ttOrder.service.UserServiceImpl;
import ttOrder.util.CookieUtil;

@Controller
@RequestMapping("/ttOrder")
public class adminController {

	@Resource
	private UserServiceImpl userService;
	
	@RequestMapping("/food")
    public String MenuManage(HttpServletRequest request,HttpServletResponse response,Model model){
		int currentRole = CookieUtil.getUserRoleFromCookie(request);
		if(currentRole == 3 || currentRole == 4){
			return "/admin/foodManage";  
		}
		else{
			return "/Backstage/nopremission";  
		}
    }
	
	@RequestMapping("/foodType")  
    public String MenuType(HttpServletRequest request,HttpServletResponse response,Model model){
		int currentRole = CookieUtil.getUserRoleFromCookie(request);
		if(currentRole == 3 || currentRole == 4){
			return "/admin/foodTypeManage"; 
		}
		else{
			return "/Backstage/nopremission";  
		}
    } 
	
	@RequestMapping("/payOrder")  
    public String payOrder(HttpServletRequest request,HttpServletResponse response,Model model){ 
		int currentRole = CookieUtil.getUserRoleFromCookie(request);
		if(currentRole == 3 || currentRole == 4){
			return "/admin/payOrder";  
		}
		else{
			return "/Backstage/nopremission";  
		}
    } 
	
	@RequestMapping("/nonePayOrder")  
    public String nonePayOrder(HttpServletRequest request,HttpServletResponse response,Model model){  
		int currentRole = CookieUtil.getUserRoleFromCookie(request);
		if(currentRole == 3 || currentRole == 4){
			return "/admin/nonePayOrder";  
		}
		else{
			return "/Backstage/nopremission";  
		}
    } 
	
	@RequestMapping("/table")  
    public String tableManage(HttpServletRequest request,HttpServletResponse response,Model model){ 
		int currentRole = CookieUtil.getUserRoleFromCookie(request);
		if(currentRole == 3 || currentRole == 4){
			return "/admin/tableManage";  
		}
		else{
			return "/Backstage/nopremission";  
		}
    } 
	
	@RequestMapping("/user")  
    public String userManage(HttpServletRequest request,HttpServletResponse response,Model model){  
		int currentRole = CookieUtil.getUserRoleFromCookie(request);
		if(currentRole == 4){
			return "/admin/userManage"; 
		}
		else{
			return "/Backstage/nopremission";  
		}
    }
	
	@RequestMapping("/login")  
    public String login(HttpSession session,HttpServletRequest request,HttpServletResponse response,Model model){  
		model.addAttribute("LoginStatus", null);
		return "/Backstage/login"; 
    } 
	
	@RequestMapping("/main")  
    public String main(HttpServletRequest request,HttpServletResponse response,Model model){
		if(CookieUtil.getUserIdFromCookie(request) != null && request.getSession().getAttribute("currentUser") == null){
			UserVO user = new UserVO();
			user.setId(CookieUtil.getUserIdFromCookie(request));
			model.addAttribute("currentUser", userService.getByKey(user));
		}
		int currentRole = CookieUtil.getUserRoleFromCookie(request);
		if(currentRole == 3 || currentRole == 4){
			return "/Backstage/main";  
		}
		else{
			return "/Backstage/nopremission";  
		}
    }
	
	@RequestMapping("/loginOut")  
    public String loginOut(HttpServletRequest request,HttpServletResponse response,Model model){ 
		model.addAttribute("LoginStatus", null);
		model.addAttribute("currentUser", null);
		CookieUtil.saveUserRoleToCookie(response,0);
		CookieUtil.saveUserIdToCookie(response, "null");
		return "/Backstage/login";  
    }
	
	@RequestMapping("/homePage")  
    public String homePage(HttpServletRequest request,HttpServletResponse response,Model model){
		if(CookieUtil.getUserIdFromCookie(request) != null && request.getSession().getAttribute("currentUser") == null){
			UserVO user = new UserVO();
			user.setId(CookieUtil.getUserIdFromCookie(request));
			model.addAttribute("currentUser", userService.getByKey(user));
		}
		return "/Backstage/homePage"; 
	}
}
